package ee.maoinvest.backend.services.implementations;

import java.sql.Connection;
import org.apache.log4j.Logger;
import ee.maoinvest.backend.database.ConnectionManager;
import ee.maoinvest.backend.model.dao.UserDAO;
import ee.maoinvest.backend.model.dao.factory.DAOFactory;
import ee.maoinvest.backend.model.data.User;
import ee.maoinvest.backend.services.UserAuthenticationService;

public class UserAuthenticationServiceImpl implements UserAuthenticationService
{
	Logger logger = Logger.getLogger(TransformerCoefficentStandardServiceImpl.class);
	//DAOFactory must definitely be  before ConnectionManager, because it initializes ConnectionManager
	//and can potentially do it here
	DAOFactory daoFactory = DAOFactory.getDAOFactory();
	UserDAO userDAOImpl;
	
	@Override
	public User getAuthenticatedUser(String username, String password) 
	{
		Connection dbConnection = ConnectionManager.getInstance().getConnection();
		userDAOImpl = daoFactory.getUserDAOImpl(dbConnection);
		User userRetrieved = null;
		
		try 
		{
			userRetrieved = userDAOImpl.getAuthenticatedUser(username, password);
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		finally
		{
			ConnectionManager.getInstance().closeConnection();
		}
		
		return userRetrieved;
	}
}
